import os
import sys
import time
import pytest
from common import shell
from common.Logs import Log
from common.send_dingtalk import send_to_dingtalk

if __name__ == "__main__":
    file = os.path.basename(sys.argv[0])
    log = Log(file)
    logger = log.Logger

    try:
        print("开始执行脚本")
        logger.info("==================================" + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + "===================================")
        pytest.main()
        #pytest.main(['F:\\Python Project\\Xiaoniu_Api_Rili\\test_case'])
        # pytest.main(['E:\\project\\Xiaoniu_Api_Rili\\test_case', '--alluredir',
        # 'E:\\project\\Xiaoniu_Api_Rili\\report\\reportallure'])
        # logger.info("脚本执行完成")
        print("脚本执行完成")
    except Exception as e:
        logger.error("脚本批量执行失败！", e)
        print("脚本批量执行失败！", e)

    try:
        shell = shell.Shell()
        cmd = 'allure generate %s -o %s --clean' % ('./reports/reportJson', './reports/reportAllure')
        # logger.info("开始执行报告生成")
        print("开始执行报告生成")
        shell.invoke(cmd)
        # logger.info("报告生成完毕")
        print("报告生成完毕")
    except Exception as e:
        print("报告生成失败，请重新执行", e)
        # logger.error("报告生成失败，请重新执行", e)
        # log.error('执行用例失败，请检查环境配置')
        raise
    time.sleep(5)
    # 发送邮件
    # mail()
    # 发送钉钉机器人
    # send_to_dingtalk()